package com.arcpan.Util;

import com.arcpan.Constant.BaseConstant;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.codec.digest.DigestUtils;


public class StringTools {
    //生成随机数
    public static final String getRandomNumber(Integer count) {
        return RandomStringUtils.random(count, false, true);
    }
    public static final String rename(String fileName){
String fileNameReal=getFileNameNoSuffix(fileName);
String suffix=getFileNameSuffix(fileName);
return  fileNameReal+"_"+getRandomNumber(BaseConstant.LENGTH_5)+suffix;
    }
    public static String getFileNameNoSuffix(String fileName) {
        Integer index = fileName.lastIndexOf(".");
        if (index == -1) {
            return fileName;
        }
        fileName = fileName.substring(0, index);
        return fileName;
    }
    public static String getFileNameSuffix(String fileName) {
        Integer index = fileName.lastIndexOf(".");
        if (index == -1) {
            return fileName;
        }
        fileName = fileName.substring(index);
        return fileName;
    }
    public static boolean isEmpty(String toString) {
        if(null == toString || "".equals(toString)||"null" == toString){
            return true;
        }else if("".equals(toString.trim())){
            return true;
        }
        return false;
    }

    public static String encodeByMd5(String password) {
        return isEmpty(password) ? null: DigestUtils.md5Hex(password);
    }

    public static boolean pathIsOk(String filePath) {
        if (StringTools.isEmpty(filePath)) {
            return true;
        }
        if (filePath.contains("../") || filePath.contains("..\\")) {
            return false;
        }
        return true;
    }
}
